/**
 * 
 */
package com.xbang.attendance.web.action.system.warehouseproduct;

import java.io.File;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.xbang.attendance.service.AnalyzeService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.struts.support.BaseActionSupport;
import com.xbang.core.utils.DateUtils;
import com.xbang.core.utils.FileUtils;

/**
 * @author dev.zhang
 * 
 */
public class Import extends BaseActionSupport {

	private static final long serialVersionUID = 4037486937918127712L;

	@Autowired
	@Qualifier("warehouseProductAnalyzeService") 
	private AnalyzeService warehouseProductAnalyzeService;

	private File file;

	private String fileFileName;

	private String fileContentType;
	private String type;
	public String execute() throws Exception {
		return "import";
	}

	/**
	 * @return
	 * @throws Exception
	 */
	@Action("/system/warehouseproduct/import/do")
	public String doImport() throws Exception {

		/** 名称是否替换 */
		boolean isReplace = false;

		String ext = fileFileName.substring(fileFileName.lastIndexOf(".")).toLowerCase();
		String newFile = "PO-".concat(DateUtils.format(new Date(), "yyyyMMddhhmmss")).concat(ext);

		File indentFile = FileUtils.uploadFile(file, ServletActionContext.getRequest().getRealPath("/upload/system/warehouseproduct"), getFileFileName(), newFile, isReplace);
		try {
			if (indentFile != null) {
				warehouseProductAnalyzeService.analyse(indentFile,type);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			setErrorMessage(e.getMessage());
			return ERROR;
		}
		
		
		
//		HttpServletRequest request = ServletActionContext.getRequest();
//		request.setAttribute("aaa", "aaaa");
//		request.getRequestDispatcher("/system/warehouseproduct/list").forward(request,ServletActionContext.getResponse());
		return "r:/system/warehouseproduct/list";
		//return null;
	}
	@Action("/system/warehouseproduct/compare/to")
	public String toCompare() throws Exception {
		return "compare";
	}
	/**
	 * @return
	 * @throws Exception
	 */
	@Action("/system/warehouseproduct/compare/do")
	public String doCompare() throws Exception {

		/** 名称是否替换 */
		boolean isReplace = false;

		String ext = fileFileName.substring(fileFileName.lastIndexOf(".")).toLowerCase();
		String newFile = "PO-".concat(DateUtils.format(new Date(), "yyyyMMddhhmmss")).concat(ext);

		File indentFile = FileUtils.uploadFile(file, ServletActionContext.getRequest().getRealPath("/upload/system/warehouseproduct"), getFileFileName(), newFile, isReplace);
//		try {
//			if (indentFile != null) {
//				warehouseProductAnalyzeService.analyse(indentFile,type);
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//			logger.error(e.getMessage());
//			setErrorMessage(e.getMessage());
//			return ERROR;
//		}
		
		try{
			String exportFile = "";
			String webPath = ServletActionContext.getRequest().getRealPath("//download//warehouse//product");
			HttpServletRequest request = ServletActionContext.getRequest();
			String urlPath = request.getRequestURL().toString();
			String contextPath = request.getContextPath();
			String contextURL = urlPath.split(contextPath)[0] + contextPath;
			exportFile = contextURL + "/download/warehouse/product/" + warehouseProductAnalyzeService.analyse(indentFile, webPath,null);
			//System.out.println(exportFile);
			return "r:" + exportFile;
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	/**
	 * @return the file
	 */
	public File getFile() {
		return file;
	}

	/**
	 * @param file
	 *            the file to set
	 */
	public void setFile(File file) {
		this.file = file;
	}

	/**
	 * @return the fileFileName
	 */
	public String getFileFileName() {
		return fileFileName;
	}

	/**
	 * @param fileFileName
	 *            the fileFileName to set
	 */
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	/**
	 * @return the fileContentType
	 */
	public String getFileContentType() {
		return fileContentType;
	}

	/**
	 * @param fileContentType
	 *            the fileContentType to set
	 */
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public AnalyzeService getWarehouseProductAnalyzeService() {
		return warehouseProductAnalyzeService;
	}

	public void setWarehouseProductAnalyzeService(AnalyzeService warehouseProductAnalyzeService) {
		this.warehouseProductAnalyzeService = warehouseProductAnalyzeService;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	
}
